<template>
  <div class="mod-config">
    <el-form :inline="true" :model="dataForm" @keyup.enter.native="search()" ref="dataForm">
      <el-form-item>
        <el-select v-model="dataForm.bigbayAppId" placeholder="请选择购买项目">
          <el-option
            v-for="item in projectList"
            :key="item.bigbayAppId"
            :label="item.bigbayAppName"
            :value="item.bigbayAppId"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-select v-model="dataForm.orderStatus" placeholder="请选择订单状态">
          <el-option v-for="item in options" :key="item.val" :label="item.label" :value="item.val"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-input v-model="dataForm.userId" placeholder="请输入uid"></el-input>
      </el-form-item>
      <el-form-item>
        <el-input v-model="dataForm.nickname" placeholder="请输入昵称"></el-input>
      </el-form-item>
      <el-form-item>
        <el-input v-model="dataForm.transactionId" placeholder="请输入交易单号"></el-input>
      </el-form-item>

      <el-form-item>
        <el-input v-model="dataForm.buyPhone" placeholder="请输入手机号"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button @click="search()">查询</el-button>
        <el-button @click="download()" type="primary">下载</el-button>
      </el-form-item>
    </el-form>
    <el-table :data="dataList" border v-loading="dataListLoading" style="width: 100%;">
      <el-table-column prop="orderId" header-align="center" align="center" label="订单号"></el-table-column>
      <el-table-column prop="transactionId" header-align="center" align="center" label="交易单号"></el-table-column>
      <el-table-column prop="gmtCreate" header-align="center" align="center" label="订单时间"></el-table-column>
      <el-table-column prop="buyUserId" header-align="center" align="center" label="购买者uid"></el-table-column>
      <el-table-column prop="buyAvator" header-align="center" align="center" label="购买者头像">
        <template slot-scope="scope">
          <img :src="scope.row.buyAvator" style="width:40px">
        </template>
      </el-table-column>
      <el-table-column prop="buyNickname" header-align="center" align="center" label="购买者昵称"></el-table-column>
      <el-table-column prop="buyPhone" header-align="center" align="center" label="购买者手机号">
        <template slot-scope="scope">
          <span v-if="scope.row.buyPhone==null">--</span>
          <span v-else>{{scope.row.buyPhone}}</span>
        </template>
      </el-table-column>
      <el-table-column prop="fromUserId" header-align="center" align="center" label="分销者uid"></el-table-column>
      <el-table-column prop="fromAvator" header-align="center" align="center" label="分销者头像">
        <template slot-scope="scope">
          <img :src="scope.row.fromAvator" style="width:40px">
        </template>
      </el-table-column>
      <el-table-column prop="fromNickname" header-align="center" align="center" label="分销者昵称"></el-table-column>
      <el-table-column
        prop="stewardshipNickName"
        header-align="center"
        align="center"
        label="购买者管家"
      >
        <template slot-scope="scope">
          <span v-if="scope.row.stewardshipNickName==null">--</span>
          <span v-else>{{scope.row.stewardshipNickName}}</span>
        </template>
      </el-table-column>
      <el-table-column prop="fromPhone" header-align="center" align="center" label="分销者手机号">
        <template slot-scope="scope">
          <span v-if="scope.row.fromPhone==null">--</span>
          <span v-else>{{scope.row.fromPhone}}</span>
        </template>
      </el-table-column>
      <el-table-column prop="orderName" header-align="center" align="center" label="购买项目"></el-table-column>
      <el-table-column prop="courseName" header-align="center" align="center" label="购买课程"></el-table-column>
      <el-table-column prop="sellPageItemName" header-align="center" align="center" label="购买页名称"></el-table-column>
      <el-table-column prop="amount" header-align="center" align="center" label="购买价格">
        <template slot-scope="scope">
          <span>{{scope.row.amount}}</span>
        </template>
      </el-table-column>
      <el-table-column prop="refundAmount" header-align="center" align="center" label="退款价格">
        <template slot-scope="scope">
          <span style="color:#f00;" v-if="scope.row.orderStatus!='1'">-{{scope.row.refundAmount}}</span>
        </template>
      </el-table-column>
      <el-table-column prop="orderStatus" header-align="center" align="center" label="订单状态">
        <template slot-scope="scope">
          <span>{{scope.row.orderStatus=='1'?'已支付':''}}</span>
          <span style="color:#f00">{{scope.row.orderStatus=='-1'?scope.row.orderType === 1?'退会员':'全部退款':''}}</span>
          <span style="color:#f00">{{scope.row.orderStatus=='-2'?'部分退款':''}}</span>
        </template>
      </el-table-column>
      <el-table-column prop="refundReason" header-align="center" align="center" label="退款原因"></el-table-column>
      <el-table-column header-align="center" align="center" label="操作">
        <template slot-scope="scope">
          <el-button v-if="scope.row.orderType === 1&&scope.row.orderStatus !== -1" type="text" size="small" @click="backMoney(scope.row.buyUserId,scope.row.orderId,scope.row.amount)">退款</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      @size-change="sizeChangeHandle"
      @current-change="currentChangeHandle"
      :current-page="pageIndex"
      :page-sizes="[10, 20, 50, 100]"
      :page-size="pageSize"
      :total="totalPage"
      layout="total, sizes, prev, pager, next, jumper"
    ></el-pagination>

    <el-dialog
      :close-on-click-modal="false"
      :visible.sync="visibleNum"
      title="退会员"
    >
      <el-form
        :model="dataForm1"
        ref="dataForm1"
        label-width="150px"
      >
        <el-form-item label="请输入退款操作" prop='immediatelyState'>
          <el-radio-group v-model="dataForm1.ifRefundMoney">
            <el-radio :label="1">退款退会员</el-radio>
            <el-radio :label="0">不退款退会员</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="请输入退款原因" prop="actionDesc">
          <el-input v-model="dataForm1.refundReason" type="textarea" :rows="4"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
      <el-button @click="visibleNum = false">取消</el-button>
      <el-button type="primary" :loading="tuiloading" :disabled="tuiload" @click="dataFormSubmit()">确定</el-button>
    </span>
    </el-dialog>
  </div>
</template>
<style>
  .disable {
    pointer-events: none;
    color: #ccc;
    text-decoration: underline;
    margin-right: 5px;
  }
  .disable .el-button--text {
    color: #ccc;
  }
  .active {
    cursor: pointer;
  }
</style>

<script>
  export default {
    data() {
      return {
        tuiloading: false,
        tuiload: false,
        dataForm1: {
          ifRefundMoney: 1
        },
        id: '',
        userId: '',
        money: '',
        visibleNum: false,
        // 会员状态
        options: [
          {
            val: "1",
            label: "已支付"
          },
          {
            val: "-1",
            label: "全部退款"
          },
          {
            val: "-2",
            label: "部分退款"
          },
          {
            val: null,
            label: "全部"
          }
        ],
        // 会员身份
        dataForm: {
          transactionId: "",
          bigbayAppId: "",
          buyPhone: "",
          userId: "",
          nickname: "",
          orderStatus:'',
        },
        isAlive: "",
        dataList: [],
        pageIndex: 1,
        pageSize: 10,
        totalPage: 0,
        dataListLoading: false,
        dataListSelections: [],
        addOrUpdateVisible: false,
        projectList: [] //
      };
    },
    activated() {
      this.getDataList();
      this.getClassList();
    },
    methods: {
      search(){
        this.pageIndex = 1;
        this.getDataList();
      },
      // 获取数据列表
      getDataList() {
        this.dataListLoading = true;
        let obj = {
          page: this.pageIndex,
          limit: this.pageSize
        };
        if (this.dataForm.transactionId) {
          obj.transactionId = this.dataForm.transactionId + "";
        }
        if (this.dataForm.buyPhone) {
          obj.buyPhone = this.dataForm.buyPhone;
        }
        if (this.dataForm.nickname) {
          obj.nickname = this.dataForm.nickname;
        }
        if (this.dataForm.userId) {
          obj.userId = this.dataForm.userId + "";
        }
        if (this.dataForm.orderStatus && this.dataForm.orderStatus != null) {
          obj.orderStatus = this.dataForm.orderStatus;
        }
        if (this.dataForm.bigbayAppId && this.dataForm.bigbayAppId != null) {
          obj.bigbayAppId = this.dataForm.bigbayAppId;
        }
        this.$http({
          url: this.$http.adornUrl("/order/orderManage/orderList"),
          method: "get",
          params: this.$http.adornParams(obj)
        }).then(({ data }) => {
          if (data && data.code === 0) {
            this.$refs["dataForm"].resetFields();
            this.dataList = data.page.list;
            this.totalPage = data.page.totalCount;
            // var sortObj = this.dataList.sort(sorting("sortIndex"));
          } else {
            this.dataList = [];
            this.totalPage = 0;
          }
          this.dataListLoading = false;
        });
      },
      getClassList() {
        this.$http({
          url: this.$http.adornUrl("/saleStatistic/AppInfo/list"),
          method: "get"
        }).then(({ data }) => {
          if (data && data.code === 0) {
            console.log(data);
            this.projectList = data.list;
          }
        });
      },
      // 每页数
      sizeChangeHandle(val) {
        this.pageSize = val;
        this.pageIndex = 1;
        this.getDataList();
      },
      // 当前页
      currentChangeHandle(val) {
        this.pageIndex = val;
        this.getDataList();
      },
      backMoney(userId, id, money) {
        this.dataForm1.refundReason = ''
        this.id = id
        this.money = money
        this.userId = userId
        this.visibleNum = true
      },
      dataFormSubmit() {
        this.tuiloading = true
        this.tuiload = true
        this.dataForm1.userId = this.userId
        this.dataForm1.orderId = this.id
        this.dataForm1.refundMoney = this.money*100
        this.$http({
          url: this.$http.adornUrl(
            `/refund/refundMemberOrder`
          ),
          method: 'post',
          data: this.$http.adornData(this.dataForm1)
        }).then(({ data }) => {
          if (data && data.code === 0) {
            this.tuiloading = false
            this.$message({
              message: '操作成功',
              type: 'success',
              duration: 1500,
              onClose: () => {
                setTimeout(()=> {
                  this.tuiload = false
                }, 1000)
                this.visibleNum = false
                this.getDataList()
              }
            });
          } else {
            this.tuiloading = false
            this.tuiload = false
            this.$message.error(data.msg)
          }
        })
      },
      download() {
        let obj = {
          page: this.pageIndex,
          limit: this.pageSize
        };
        if (this.dataForm.transactionId) {
          obj.transactionId = this.dataForm.transactionId + "";
        }
        if (this.dataForm.buyPhone) {
          obj.buyPhone = this.dataForm.buyPhone;
        }
        if (this.dataForm.nickname) {
          obj.nickname = this.dataForm.nickname;
        }
        if (this.dataForm.userId) {
          obj.userId = this.dataForm.userId + "";
        }
        if (this.dataForm.orderStatus && this.dataForm.orderStatus != null) {
          obj.orderStatus = this.dataForm.orderStatus;
        }
        if (this.dataForm.bigbayAppId && this.dataForm.bigbayAppId != null) {
          obj.bigbayAppId = this.dataForm.bigbayAppId;
        }
        this.$http({
          url: this.$http.adornUrl("/order/orderManage/orderList/download"),
          method: "get",
          params: this.$http.adornParams(obj),
          headers: {
            'Content-Type': 'application/json; application/octet-stream'
          },
          responseType: 'arraybuffer'
        }).then(({ data }) => {
          const blob = new Blob([data], { type: 'application/vnd.ms-excel' })
          const downloadElement = document.createElement('a')
          const href = window.URL.createObjectURL(blob)
          downloadElement.href = href
          downloadElement.download = '订单.xls'
          document.body.appendChild(downloadElement)
          downloadElement.click()
          document.body.removeChild(downloadElement) // 下载完成移除元素
          window.URL.revokeObjectURL(href) // 释放掉blob对象
        })
      }
    }
  };
</script>


